﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _5_LongestPalindrome
{
    class Program
    {
        static void Main(string[] args)
        {
            string test = "abba";
            string result  = LongestPalindrome(test);

            Console.WriteLine(result);

            Console.ReadLine();
                
        }

        public static string LongestPalindrome(string s)
        {
            string result = string.Empty;

            if (s.Length < 2)
            {
                result = s;
            }
            else
            {
                List<string> list = new List<string>();

                foreach (char c in s)
                {

                    list.Add(c.ToString());

                    for (int i = 0; i <list.Count(); i++)
                    {
                        if (i != list.Count()-1)
                        {
                            if (list[i].StartsWith(c.ToString()) && result.Length < (list[i] + c).Length)
                            {
                                result = list[i] + c;
                            }

                            if (list[i].IndexOf(c) > -1)
                            {
                                list.Remove(list[i]);
                            }
                            else
                            {
                                list[i] = list[i] + c;
                            }
                        }
                    }
                }
            }
            return result;
        }
    }
}
